package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.integral.IntegralDto;
import com.java110.intf.acct.IIntegralSettingV1InnerServiceSMO;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("queryAppUserIntegralBmoImpl")
public class QueryAppUserIntegralBmoImpl implements IMallCommonApiBmo {

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;

    @Autowired
    private IIntegralSettingV1InnerServiceSMO integralSettingV1InnerServiceSMOImpl;


    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "link", "未包含手机号");
    }

    @Override
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {


        IntegralDto integralDto = new IntegralDto();
        integralDto.setLink(reqJson.getString("link"));
        integralDto.setObjType(IntegralDto.OBJ_TYPE_PERSON);
        List<IntegralDto> integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);
        JSONObject paramOut = new JSONObject();
        int integral = 0;
        if (!ListUtil.isNull(integralDtos)) {
            integral = Integer.parseInt(integralDtos.get(0).getAmount());
        }
        paramOut.put("integral", integral);

        //todo 计算成金额
        double money = integralSettingV1InnerServiceSMOImpl.computeMoney(integral);
        paramOut.put("integralMoney", money);

        context.setResponseEntity(ResultVo.createResponseEntity(paramOut));
    }
}
